#!/usr/bin/env python

"""
    GameMap.py
    This file contains procedures required for generating map.
"""

# Imports
import random
from GameUI import *

class mapTile(object):
    def __init__(self, char, color, walkable = True, block_los = False):
        self.char = char
        self.color = color
        self.walkable = walkable
        self.block_los = block_los
        self.fov = False
        self.visited = False

class gameMap(object):
    def __init__(self, height, width, gameUI, gameObjects):
        self.height = height
        self.width = width
        self.gameUI = gameUI
        self.gameObjects = gameObjects
        self.trees = 0

        self.map = [[ mapTile('.', 2)
            for x in range(self.width) ]
                for y in range(self.height) ]

    def genMap(self):
        for y in range(self.height):
            for x in range(self.width):
                if y == 0 or y == self.height-1 or x == 0 or x == self.width-1:
                    self.map[y][x] = mapTile('^', 0, False, True)
                if y == 0 and (x == self.width-6 or x == self.width-7):
                    self.map[y][x] = mapTile('~', 1, False, False)
                if (y == 1 or y == 7 or y == 8) and (x == self.width-7 or x == self.width-8):
                    self.map[y][x] = mapTile('~', 1, False, False)
                if (y == 2 or y == 3 or y == 5 or y == 6) and (x == self.width-8 or x == self.width-9):
                    self.map[y][x] = mapTile('~', 1, False, False)
                if y == 9 and (x == self.width-6 or x == self.width-7 or x == self.width-8):
                    self.map[y][x] = mapTile('~', 1, False, False)
                if y == 10 and (x > self.width-8):
                    self.map[y][x] = mapTile('~', 1, False, False)
                if (y == 11 or y == 12) and (x > self.width-6):
                    self.map[y][x] = mapTile('~', 1, False, False)
                if (y == 13 or y == 14) and (x > self.width-7):
                    self.map[y][x] = mapTile('~', 1, False, False)
                if (y == 1) and (x > self.width-7 and x < self.width-1):
                    self.map[y][x] = mapTile('#', 0, False, True)
                if (y == 2 or y == 6) and (x == self.width-7 or x == self.width-6 or x == self.width-4 or x == self.width-2):
                    self.map[y][x] = mapTile('#', 0, False, True)
                if (y == 3 or y == 5) and (x == self.width-7 or x == self.width-4 or x == self.width-2):
                    self.map[y][x] = mapTile('#', 0, False, True)
                if y == 4 and x == self.width-2:
                    self.map[y][x] = mapTile('#', 0, False, True)
                if y == 6 and x == self.width-3:
                    self.map[y][x] = mapTile('#', 0, False, True)
                if y == 7 and x == self.width-6:
                    self.map[y][x] = mapTile('#', 0, False, True)
                if y == 8 and (x == self.width-6 or x == self.width-5):
                    self.map[y][x] = mapTile('#', 0, False, True)
                if y == 9 and x == self.width-5:
                    self.map[y][x] = mapTile('#', 0, False, True)
                if y == 4 and x > self.width-10 and x < self.width-2:
                    self.map[y][x] = mapTile('.', 0, True, False)
                if (y == 3 or y == 5) and x == self.width-3:
                    self.map[y][x] = mapTile('.', 0, True, False)
                if y == 4 and x == self.width-4:
                    self.map[y][x] = mapTile('/', 6, True, False)
                if y == 2 and x == self.width-3:
                    self.map[y][x] = mapTile('-', 6, True, False)

        for y in range(self.height):
            for x in range(self.width):
                if self.map[y][x].char == '.' and self.map[y][x].color == 2:
                    if random.randint(0, 100) > 65:
                        self.map[y][x] = mapTile('T', 2, False, True)
                        self.trees += 1

    def drawMap(self):
        for y in range(self.height):
            for x in range(self.width):
                if self.map[y][x].fov == True:
                    self.gameUI.putChar(y, x, self.map[y][x].char, self.map[y][x].color, False)
                elif self.map[y][x].visited == True:
                    self.gameUI.putChar(y, x, self.map[y][x].char, 3, False)
                else:
                    self.gameUI.putChar(y, x, ' ', False)
        for obj in self.gameObjects:
            if self.map[obj.posy][obj.posx].fov == True:
                obj.draw()